﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EasyCLRBase
{
    /// <summary>
    /// Manage Enumerator structure information.
    /// </summary>
    public struct EnumDefinition
    {
        /// <summary>
        /// Value of the current Enumerator entry
        /// </summary>
        public Object Value;
        /// <summary>
        /// Name of the current enumerator entry
        /// </summary>
        public String Name;

        public override string ToString()
        {
            return String.Concat(this.Value, ": ", this.Name);
        }
    }
    public static class EnumHelper
    {
        /// <summary>
        /// Extension that returns enumerator information
        /// </summary>
        /// <param name="enumerator"></param>
        /// <returns>All entries of the enumerator by name and value.</returns>
        public static EnumDefinition[] GetEnumeratorDefinition(this Enum enumerator)
        {
            var enumType = enumerator.GetType();
            var tcode = enumerator.GetTypeCode();
            var list = Enum.GetNames(enumType);
            var result = new EnumDefinition[list.Length];
            for (int i = 0; i < list.Length; i++)
            {
                result[i] = new EnumDefinition { Name = list[i], Value = Convert.ChangeType(Enum.Parse(enumType, list[i]), tcode) };
            }
            return result;
        }

        public static String[] GetEnumeratorDefinitionString(this Enum enumerator)
        {
            var list = enumerator.GetEnumeratorDefinition();
            var result = new String[list.Length];
            int i =0;
            foreach (var entry in list)
            {
                result[i++] = entry.ToString();
            }
            return result;
        }
    }
}
